﻿using S7.Net;
using S7.Net.Types;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TYBase;
using TYPLC.Omron;
using TYPLC.PLC;
using TYRTUBase;

namespace TYPLC.S7
{
    public class S7PLC_1500 : IPLCBase<DataItem>
    {
        Plc plc = null;

        public S7PLC_1500(string ip) : base()
        {
            plc = new Plc(CpuType.S71500, ip, 0, 0);
        }

        public override bool Connect()
        {
            try
            {
                if (plc == null)
                    return false;
                plc.Open();
                return true;
            }
            catch (Exception ex)
            {
                Log.E("connect error", ex);
                return false;
            }
        }

        public override bool AddressAs(string _strAddress, out DataItem data)
        {
            try
            {
                data = DataItem.FromAddress(_strAddress);
                return true;
            }
            catch (Exception)
            {
                data = null;
                return false;
            }
        }


        public override bool ReadPri(DataItem address, out object value)
        {
            try
            {
                if (address.VarType == VarType.String)
                {
                    int startString = address.StartByteAdr + 2;
                    value = plc.Read(address.DataType, address.DB, startString, VarType.String, address.Count);
                    value = ((string)value).Replace("\0", "");
                }
                else
                {
                    value = plc.Read(address.DataType, address.DB, address.StartByteAdr, address.VarType, address.Count, address.BitAdr);
                }
                return true;
            }
            catch (Exception ex)
            {
                value = null;
                return false;
            }
        }

        public override bool WritePri(DataItem address, object value)
        {
            try
            {
                if (address.VarType == VarType.Byte)
                {
                    plc.Write(address.DataType, address.DB,
                                        address.StartByteAdr, Convert.ToByte(value),
                                        -1);
                    return true;
                }
                if (address.VarType == VarType.DWord)
                {
                    plc.Write(address.DataType, address.DB,
                                        address.StartByteAdr, Convert.ToSingle(value),
                                        -1);
                    return true;
                }
                plc.Write(address.DataType, address.DB,
                    address.StartByteAdr, value,
                    address.BitAdr);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
